package zjc.bookkeeping.tools;

import android.app.Dialog;
import android.content.Context;
import android.content.ContextParams;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import java.lang.reflect.Field;

import zjc.bookkeeping.R;


public class SelectTimeDialog extends Dialog implements View.OnClickListener{
    DatePicker datePicker;
    Button sureBtn,cancelBtn;
    TextView ymdSelect,ymSelect,dayTv;
    String time,monthStr,dayStr;
    int year,month,dayOfMonth;
    boolean kind = false;

    public interface OnTimeSelectSureListener{
        public void onTimeSelectSure(String time,int year,int month,int day);
    }
    OnTimeSelectSureListener onTimeSelectSureListener;

    public void setOnTimeSelectSureListener(OnTimeSelectSureListener onTimeSelectSureListener) {
        this.onTimeSelectSureListener = onTimeSelectSureListener;
    }

    public SelectTimeDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select_time);
        initView();
    }

    private void getTime() {
        year = datePicker.getYear();
        month = datePicker.getMonth() + 1;
        dayOfMonth = datePicker.getDayOfMonth();
        monthStr = String.valueOf(month);
        if (month < 10){
            monthStr = "0" + month;
        }
        dayStr = String.valueOf(dayOfMonth);
        if (dayOfMonth < 10){
            dayStr = "0" + dayOfMonth;
        }
    }

    private void initView() {
        datePicker = findViewById(R.id.dialog_select_time_dp);
        sureBtn = findViewById(R.id.dialog_select_time_sure);
        cancelBtn = findViewById(R.id.dialog_select_time_cancel);
        ymdSelect = findViewById(R.id.dialog_select_time_ymd);
        ymSelect = findViewById(R.id.dialog_select_time_ym);
        dayTv = findViewById(R.id.dialog_select_time_day);

        hideDay(datePicker);
        dayTv.setVisibility(View.GONE);

        sureBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        ymdSelect.setOnClickListener(this);
        ymSelect.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.dialog_select_time_sure:
                getTime();
                if (kind){
                    time = year + "年" + monthStr + "月" + dayStr + "日";
                }else {
                    time = year + "年" + monthStr + "月";
                }
                onTimeSelectSureListener.onTimeSelectSure(time,year,month,dayOfMonth);
                Log.i("时间：",time);
                cancel();
                break;
            case R.id.dialog_select_time_cancel:
                cancel();
                break;
            case R.id.dialog_select_time_ymd:
                showDay(datePicker);
                dayTv.setVisibility(View.VISIBLE);
                setupColor();
                ymdSelect.setBackgroundColor(Color.parseColor("#729FCF"));
                kind = true;
                break;
            case R.id.dialog_select_time_ym:
                hideDay(datePicker);
                dayTv.setVisibility(View.GONE);
                setupColor();
                ymSelect.setBackgroundColor(Color.parseColor("#729FCF"));
                kind = false;
                break;
        }
    }
//    设置两个模式按钮控件的背景颜色
    private void setupColor() {
        ymdSelect.setBackgroundColor(Color.parseColor("#C5E0FB"));
        ymSelect.setBackgroundColor(Color.parseColor("#C5E0FB"));
    }
//    设置Dialog控件显示位置
    public void setRemarksDialogSize(){
        Window window =getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        Display display = window.getWindowManager().getDefaultDisplay();
        layoutParams.width = (int) (display.getWidth());
        layoutParams.gravity = Gravity.BOTTOM;
        window.setAttributes(layoutParams);
    }
//    显示时间选择器中的日控件
    private void showDay(DatePicker datePicker) {
        int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
        if (daySpinnerId != 0) {
            View daySpinner = datePicker.findViewById(daySpinnerId);
            if (daySpinner != null) {
                daySpinner.setVisibility(View.VISIBLE);
            }
        }
    }
//    隐藏时间选择器中的日控件
    private void hideDay(DatePicker mDatePicker) {
        int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
        if (daySpinnerId != 0) {
            View daySpinner = datePicker.findViewById(daySpinnerId);
            if (daySpinner != null) {
                daySpinner.setVisibility(View.GONE);
            }
        }
    }
}
